package com.xiaoy;

public class IdCardUtil {

	
	
	private String reg1 = "/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/";
	private String reg2 = "/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/";
	
	private int[] x = {0,1,2,3,4,5,6,7,8,9,10};
	private String[] y = {"1","0","x","9","8","7","6","5","4","3","2"};
	private int[] wis = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	
	public boolean vIdCard (String idCard) {
		
		if(!idCard.matches(reg1)&&!idCard.matches(reg2)) {
			return false;
		}
		
//		if (!reg1.matches(idCard)&&reg2.matches(idCard)) {
//			return false;
//		}
		
		 String checkNo = last(idCard);
		 //获取身份证号码最后一位
		 String endNO=idCard.substring(17,18);
		 if(!checkNo.equals(endNO)){
			 return false;
		 }
		return true;
	}
	
	public String last(String idCard) {

		int ids = 0;
		int wi = 0;
		String checkNo = "";
		int idswisum = 0;

		// 循环遍历身份证，获取身份证每个单号与加权因子对应相乘之和除以11的余数
		for (int i = 0; i < 17; i++) {
			ids = Integer.parseInt(idCard.substring(i, i + 1));
			wi = wis[i];
			idswisum = ids * wi + idswisum;
		}
		int remainder = idswisum % 11; // 之和余数
		for (int i = 0; i < 11; i++) {
			if (remainder == x[i]) {
				checkNo = y[i];
			}
		}

		return checkNo;
	}
	
	
	public static void main(String[] args) {
		String idCard = "43012219890710401x";
		String lastCard = "430122197408094019";
		
		IdCardUtil id = new IdCardUtil();
//		idCard = "430122198888884013";
		
		System.out.println(id.last(lastCard));
		
		System.out.println(id.vIdCard(idCard));
	}
	
	
	
	
}
